let questionApp=new Vue({
    el:"#questionApp",
    data:{
        question:{}
    },
    methods:{
        loadQuestion:function(){
            //URL路径上?之后的内容可以用下面的代码获得
            let qid=location.search;
            console.log(qid);
            //如果qid不存在
            if(!qid){
                alert("必须指定问题id")
                return;
            }
            //因为qid中实际是包含?的 既 ?154
            //如果想获得154这个id号,需要把开始的?去掉
            //将qid中开头的?去掉
            qid=qid.substring(1);
            axios({
                //  /v1/questions/154
                url:"/faq/v1/questions/"+qid,
                method:"get"
            }).then(function(response){
                questionApp.question=response.data;
                addDuration(questionApp.question);
            })
        }

    },
    created:function(){
        this.loadQuestion();
    }
})

let postAnswerApp=new Vue({
    el:"#postAnswerApp",
    data:{
        hasError:false,
        message:""
    },
    methods:{
        postAnswer:function(){
            let qid=location.search;
            if(!qid){
                this.hasError=true;
                this.message="必须提供问题id";
                return;
            }
            //去掉?
            qid=qid.substring(1);
            let content=$("#summernote").val();
            if(!content){
                this.hasError=true;
                this.message="回答内容不能为空";
                return;
            }
            let form=new FormData();
            form.append("questionId",qid);
            form.append("content",content);
            axios({
                url:"/faq/v1/answers",
                method:"post",
                data:form
            }).then(function(response){
                //新增成功将回答对象追加到回答列表中
                answersApp.answers.push(response.data);
                //清空summernote
                $("#summernote").summernote("reset");
                //设置新增的回答持续时间为刚刚
                response.data.duration="刚刚";
            })

        }
    }
})

let answersApp=new Vue({
    el:"#answersApp",
    data:{
        answers:[]
    },
    methods:{
        loadAnswers:function(){
            let qid=location.search;
            if(!qid){
                return;
            }
            qid=qid.substring(1);
            axios({
                url:"/faq/v1/answers/question/"+qid,
                method:"get"
            }).then(function(response){
                answersApp.answers=response.data;
                answersApp.updateDuration();
            })
        },
        updateDuration:function(){
            let answers=this.answers;
            for(let i=0;i<answers.length;i++){
                addDuration(answers[i]);
            }
        },
        postComment:function(answerId){
            let textarea=$("#addComment"+answerId+" textarea")
            let content=textarea.val();
            if(!answerId || !content){
                return;
            }
            let form=new FormData();
            form.append("answerId",answerId);
            form.append("content",content);
            axios({
                url:"/faq/v1/comments",
                method:"post",
                data:form
            }).then(function(response){
                console.log(response.data);
                //开始遍历
                let answers=answersApp.answers;
                for(let i=0;i<answers.length;i++){
                    if(answers[i].id==answerId){
                        answers[i].comments.push(response.data);
                        break;
                    }
                }
                //情况文本框内容
                textarea.val("");
                $("#addComment"+answerId).collapse('hide');

            })
        },
        removeComment:function (commentId,index,comments) {
            if(!commentId)
                return;
            axios({
                //  /v1/comments/23/delete
                url:"/faq/v1/comments/"+commentId+"/delete",
                method:"get"
            }).then(function(response){
                console.log(response.data);
                if(response.data=="ok"){
                    //splice方法有两个参数(a,b),它的含义为
                    //从数组的a位置开始删除b个元素
                    //如果不指定b删除a之后所有元素
                    comments.splice(index,1);
                }else{
                    alert(response.data);
                }
            })
        },
        updateComment:function(commentId,answerId,index,
                               comments){
            let textarea=$("#editComment"+commentId+" textarea");
            let content=textarea.val();
            if(!answerId||!content){
                console.log("answerId或content不存在");
                return;
            }
            let form=new FormData();
            form.append("answerId",answerId);
            form.append("content",content);
            axios({
                url:"/faq/v1/comments/"+commentId+"/update",
                method:"post",
                data:form
            }).then(function(response){
                console.log(response.data);
                //将指定位置的对象修改效果显示
                //comments[index]=response.data;
                //当我们编写vue代码进行内容更新时,遇到更新不在页面上显示效果的情况时
                //尝试使用下面代码
                //set([要更新的数组],[更新第几个元素下标],[更新成什么])
                Vue.set(comments,index,response.data);
                //把当前的编辑框折叠起来
                $("#editComment"+commentId).collapse("hide");
            })
        },
        answerSolved:function(answerId){
            if(!answerId){
                return;
            }
            axios({
                url:"/faq/v1/answers/"+answerId+"/solved",
                method:"get"
            }).then(function(response){
                console.log(response.data);
                if(response.data!="ok"){
                    alert(response.data);
                }
            })
        }
    },
    created:function(){
        this.loadAnswers();
    }
})




